﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.DataModel;
using Giaiphongmatbang.BLL;

namespace Giaiphongmatbang.UserControl
{
    public partial class ucSupportRent : System.Web.UI.UserControl
    {
        int ProjectID
        {
            get { return (int)ViewState["ProjectID"]; }
            set { ViewState["ProjectID"] = value; }
        }
        int HouseHoldID
        {
            get { return (int)ViewState["CMND"]; }
            set { ViewState["CMND"] = value; }
        }
        bool modeEdit
        {
            get { return (bool)ViewState["modeEdit"]; }
            set { ViewState["modeEdit"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                modeEdit = false;
                try
                {
                    ProjectID = int.Parse(Request.QueryString["pID"]);
                    HouseHoldID = int.Parse(Request.QueryString["hID"]);
                    modeEdit = true;
                }
                catch { }
            }
        }
        SupportRentRepository repository = new SupportRentRepository();
        SupportsRepository sp = new SupportsRepository();
        public void BindData()
        {
            StringBuilder str = new StringBuilder("<table id='treeRent' style='padding-left:19px;' width='100%' class='table'>");
            str.AppendLine("<tr>"
                + "<th>Loại thuê nhà</th>"
                + "<th width='100px'>Đơn vị tính</th>"
                + "<th width='100px'>Đơn giá</th>"
                + "<th width='100px'>Số lượng</th>"
                + "<th width='70px'>Lựa chọn</th>"
                + "</tr>");
            var list = repository.GetByParentID(0);
            foreach (var item in list)
            {
                StringBuilder node = new StringBuilder("<tr id='" + item.SupporRenttID + "'><td>" + item.SupportName + "</td><td>" + item.Unit + "</td><td>"+item.UnitPrice+"</td>");
                if (!repository.HasChild(item.SupporRenttID))
                {
                    if (modeEdit && repository.checkSupport(ProjectID, HouseHoldID, item.SupporRenttID))
                    {
                        int QuantityRent = sp.GetByKey(ProjectID, HouseHoldID).QuantityRent.Value;
                        node.AppendLine("<td><input type='text' class='field' value='"+ QuantityRent +"'></td><td><input type='checkbox' checked='checked'/></td></tr>");
                    }
                    else
                        node.AppendLine("<td><input type='text' class='field'/></td><td><input type='checkbox'/></td></tr>");
                }
                else
                {
                    node.AppendLine("<td>&nbsp;</td><td>&nbsp;</td></tr>");
                }
                BindDataChild(node, item.SupporRenttID);
                str.AppendLine(node.ToString());
            }
            str.AppendLine("</table>");
            ltr.Text = str.ToString();

        }
        protected void BindDataChild(StringBuilder parent, int ParentID)
        {
            var list = repository.GetByParentID(ParentID);

            foreach (var item in list)
            {
                StringBuilder node = new StringBuilder("<tr id='" + item.SupporRenttID + "' class='child-of-" + item.ParentID + "'><td>" + item.SupportName + "</td><td>" + item.Unit + "</td><td>" + item.UnitPrice + "</td>");
                if (!repository.HasChild(item.SupporRenttID))
                {
                    if (modeEdit && repository.checkSupport(ProjectID, HouseHoldID, item.SupporRenttID))
                    {
                        int QuantityRent = sp.GetByKey(ProjectID, HouseHoldID).QuantityRent.Value;
                        node.AppendLine("<td><input type='text' class='field' value='" + QuantityRent + "'></td><td><input type='checkbox' checked='checked'/></td></tr>");
                    }
                    else
                        node.AppendLine("<td><input type='text' class='field'/></td><td><input type='checkbox'/></td></tr>");
                }
                else
                {
                    node.AppendLine("<td>&nbsp;</td><td>&nbsp;</td></tr>");
                }
                BindDataChild(node, item.SupporRenttID);
                parent.AppendLine(node.ToString());
            }
        }  
    }
}